package com.tmxk.controller.dept;

import com.tmxk.constant.Result;
import com.tmxk.po.SysDept;
import com.tmxk.service.dept.DeptService;
import com.tmxk.utils.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/sysDept")
@Api(tags = "部门管理")
public class DeptController {
    @Resource
    private DeptService deptService;

    @GetMapping("/dept")
    @ApiOperation(notes = "查询部门所有信息",value = "查询部门所有信息")
    public ResultData queryAllDept(){
        List<SysDept> sysDepts =  deptService.queryAllDept();
        return new ResultData<>(Result.SUCCESS.getCode(), "",sysDepts.size(),sysDepts) ;
    }

    @GetMapping("/dept/{deptId}")
    @ApiOperation(notes = "根据部门ID查询部门信息",value = "根据部门ID查询部门信息")
    public ResultData getDeptById(@PathVariable("deptId") Integer deptId){
        SysDept sysDept = deptService.queryDeptById(deptId);
        return new ResultData<>(Result.SUCCESS.getCode(), "", Arrays.asList(sysDept)) ;
    }

    @ApiOperation(notes = "根据部门ID删除部门信息",value = "根据部门ID删除部门信息")
    @DeleteMapping("/dept/{deptId}")
    public ResultData deleteDeptById(@PathVariable("deptId") Integer deptId){
        int code =  deptService.deleteSysDeptById(deptId);
        code = code > 0 ? 0 : code;
        return new ResultData<>(code, "删除成功" );
    }

    @ApiOperation(notes = "录入部门信息",value = "录入部门信息")
    @PostMapping("/dept")
    public ResultData saveDept(@RequestBody SysDept sysDept){
        //非空校验
        if(StringUtils.isEmpty(sysDept.getDeptName())){
            return new ResultData<>(Result.OTHER.getCode(), "部门名称不能为空" );
        }

        if(StringUtils.isEmpty(sysDept.getOrderNum())){
            return new ResultData<>(Result.OTHER.getCode(), "部门排序不能为空" );
        }
        int code = deptService.saveSysDept(sysDept);
        code = code > 0 ? 0 : code;
        return new ResultData<>(code, "保存成功" );
    }

    @ApiOperation(notes = "更新部门信息",value = "更新部门信息")
    @PutMapping("/dept")
    public ResultData updateSysDept(@RequestBody SysDept sysDept){
        int code = deptService.updateSysDept(sysDept);
        code = code > 0 ? 0 : code;
        return new ResultData<>(code, "更新成功" );
    }
}
